package 反转链表;

import 其他.DataStructure.ListNode;
import org.junit.Test;

import static MyUtils.MHUtils.*;

/*
    做完题一定写入表格

    pq应该是index，我当成val了
    1建dm边界好处理
    2画图
*/
public class b_基本2_反转指定pq间的链表92 {

    //工具：反转左右为null切好的链表
    public ListNode fanzhuan_2bianqiehaoweinull(ListNode h) {
        ListNode fir = h;
        if(h==null){
            return null;
        }
        ListNode sec = h.next;
        if(h.next==null){
            return h;
        }
        while(sec!=null){
            ListNode tmp = sec.next;
            sec.next = fir;
            fir=sec; sec=tmp;
        }
        h.next = null;
        return fir;
    }
    public ListNode reverseBetween(ListNode h, int p, int q) {
        ListNode c = h;
        while(c.next.val!=p){
            c=c.next;
        }
        ListNode pL = c;
        ListNode P = pL.next;
        while(c.val!=q){
            c=c.next;
        }
        ListNode Q = c;
        ListNode qR = c.next;

        pL.next=null; Q.next=null;
        fanzhuan_2bianqiehaoweinull(P);
        pL.next=Q;
        P.next=qR;

        return h;
    }

    @Test
    public void test() {
        ListNode h = lizi_lianbiao("1 2 3 4 5");
        reverseBetween(h,2,4);
    }
}
